<?php
namespace Home\Controller;
use Think\Controller;

Class TrialController extends CommonController{

    public function _initialize() {
        parent::_initialize();
        $this->dbname = CONTROLLER_NAME;
    }

    public function index(){
        $now = date('Y-m',time());
        if(IS_POST){
            $time = substr($_POST['time1'],0,7);
            if($time >= $now){
                $cause = '本月的数据统计暂时不能查看';
                $time1 = strtotime("-1 month");
            }else{
                $time1 = strtotime("-1 month");
            }
            $this->assign('uid',$_REQUEST['username']);
            $uid = $_REQUEST['username'];
        }else{
            $uid = 20;
            $this->assign('uid',20);
            $time1 = strtotime("-1 month",time());
        }
        $search = $this->search($uid,date('Y-m',$time1));
        if(!empty($search)){
            $details = $search;
        }else{
            $details = $this->shenhe($uid,date('Y-m',$time1));
        }
        $this->assign('data',$time1);
        $this->assign('cause',$cause);
        $this->assign('details',$details);
        $this->display();
    }
 
    public function search($uid,$data){
        $model = M('cash_shenhe');
        $where['uid'] = $uid;
        $where['data'] = $data;
        $result = $model->where($where)->find();
        return $result;
    }

    public function type(){
        $uid = $_GET['uid'];
        $time = $_GET['data'];
        $now = date('Y-m',time());
        if(date('Y-m',$time) >= $now){
            $time1 = strtotime("-1 month");
            $data = date('Y-m',$time1);
        }else{
            $data = date('Y-m',$time);
        }
        $where['uid'] = $uid;
        $where['data'] = $data;
        $val = M('cash_shenhe')->where($where)->find();
        $this->graph('初审','初审统计',1,$val);
    }


    public function graph($fd,$ft,$type,$val){
        import("Org.Util.Chart");
        $chart = new \Chart;
        $title = $ft; 
        $data = array($val['fkpass'],$val['fkhui'],$val['lg'],$val['gt']); 
        $size = 140; 
        $width = 550; 
        $height = 300; 
        $legend = array('未回款','回款','LT','GT');
        ob_end_clean();
         if ($type == 1) {
            $chart->create3dpie($title,$data,$size,$height,$width,$legend);
         }
         if ($type == 2) {
            $chart->createcolumnar($title,$data,$size,$height,$width,$legend);
         }
         if ($type == 3) {
            $chart->createmonthline($title,$data,$size,$height,$width,$legend);
         }
         if ($type == 4) {
            $chart->createring($title,$data,$size,$height,$width,$legend);
         }
         if ($type == 5) {
            $chart->createhorizoncolumnar($title,$subtitle,$data,$size,$height,$width,$legend);
         }
    }

    public function shenhe($uid,$mon){

        $endtime = strtotime($mon.'-'.'01 +1 month');

        $order = M('cash_order')->where("login_id = {$uid} AND creat_time < {$endtime} AND already <> 5 AND already <> -1")->select();

        $chu = $this->chuPass($order);

        $fk = $this->fk($chu['pass'],$mon,$uid);
        return $fk;
    }

    public function chuPass($order){
        for($i=0;$i<count($order);$i++){
            if($order[$i]['vstatus'] == 1){
                $chushen['pass'][] = $order[$i];
            }elseif($order[$i]['vstatus'] == -1){
                $chushen['nopass'][] = $order[$i];
            }
        }

        return $chushen;
    }


    public function fk($fk,$mon,$uid){
        $fkDetails = $this->fkDetails($fk);
        $val = M('cash_order')->where("already = 4 AND login_id = {$uid}")->select();
        $monthOrderF = $this->monthOrderF($val);
        $numH = $this->numH($monthOrderF['lt']);
        $money = count($fkDetails['fkPass']) + count($fkDetails['fkHui']) + count($numH) - (count($monthOrderF['gt']) * 50);

        //获取当前所属人的姓名
        $uname = M('user')->where("id = {$uid}")->field('truename')->find();
        $result['uname'] = $uname['truename'];
        $result['fkPass'] = count($fkDetails['fkPass']);
        $result['fkHui'] = count($fkDetails['fkHui']);
        $result['lg'] = count($numH);      
        $result['gt'] = count($monthOrderF['gt']);
        $result['money'] = $money;
        $result['data'] = $mon;
        $result['uid'] = $uid;
        $result['creat_time'] = time();
        $int = M('cash_shenhe')->add($result);
        return $result;

    }

    public function fkDetails($fk){
         for($k=0;$k<count($fk);$k++){ 
            if($fk[$k]['status'] == 4){
                if($fk[$k]['already'] == 4){
                    $fkDetails['alreadyH'][] = $fk[$k];
                }else{
                    $fkDetails['str'] .= $fk[$k]['order_id'].',';
                    $fkDetails['fkPass'][] = $fk[$k];
                }         
            }elseif($fk[$k]['status'] == 5 && $fk[$k]['already'] != 4){
                $fkDetails['str2'] .= $fk[$k]['order_id'].',';
                $fkDetails['fkHui'][] = $fk[$k];
            }elseif($fk[$k]['status'] == -2){
                $fkDetails['fkNopass'][] = $fk[$k];
            }else{
                $fkDetails['other'][] = $fk[$k];
            }
        }
        $this->already1($fkDetails['str']);
        $this->already2($fkDetails['str2']);
        return $fkDetails;
    }

    public function already1($str){
        $wheresave['order_id'] = ['IN',explode(',',$str)];
        $save = M('cash_order')->where($wheresave)->save(['already'=>4]);
    }

    public function already2($str2){
        $where['order_id'] = ['IN',explode(',',$str2)];
        $save = M('cash_order')->where($where)->save(['already'=>5]);
    }

    //已经查询过的订单
    public function monthOrderF($val){
        for($k=0;$k<count($val);$k++){
            $k_time = M('cash_bf_dk')->where("order_id = {$val[$k]['order_id']}")->field('k_time')->order('k_time desc')->find();

            if($k_time['k_time'] > 0){
                $day = (($k_time['k_time']-$val[$k]['loan_time']) / 86400);
            }else{
                $day = ((time()-$val[$k]['loan_time']) / 864000);
            }

            if($day >= 30){
                $order_id .= $val[$k]['order_id'].','; 
                $arr['gt'][] = $val[$k];
            }else{
                $arr['lt'][] = $val[$k];
            }
        }
        $where['order_id'] = ['IN',explode(',',$order_id)];
        $save = M('cash_order')->where($where)->save(['already' => -1]);
        return $arr;
    }
   
    //已经查询过的订单  之后还款的订单
    public function numH($hk){
        for($h=0;$h<count($hk);$h++){
            if($hk[$h]['status'] == 5){
                $order_id .= $hk[$h]['order_id'].',';
                $result[] = $hk[$h];
            }
        }
        $wheresave['order_id'] = ['IN',explode(',',$order_id)];
        $save = M('cash_order')->where($wheresave)->save(['already'=>5]);
        return $result;
    }



    //导出所有人的数据
    public function outexcel(){
        $where['posname'] = '风控审核专员';
        $where['status'] = 1;
        $arr = M('user')->where($where)->field('id')->select();

        $where['data'] = date('Y-m',$_REQUEST['time']);

        foreach($arr as $k=>$v){
            $where['uid'] = $v['id'];
            $result = M('cash_shenhe')->join('xy_user ON cash_shenhe.uid = xy_user.id')->where($where)->field('xy_user.truename,fkPass,fkHui,lg,gt,money')->find();
            if($result){
                $val[] = $result;
            }else{
                $val[] = 0;
            }
        }
        //$str = implode(',',$val);
        $valArr = array('姓名','未回款','已回款','小于30天','大于30天','总钱数');
        $filename = date('Y-m',$_REQUEST['time']).'月初审订单详情';
        $this->xlsout($filename,$valArr,$val);
    }

}